﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using XVUEfxSDK.Xsequencer;
using XVUEfxSDK.Xplugin.Xuserinterface;

namespace HelperAllSamplesUserInterface
{
    public partial class MainUserControlSamples : UserControl
    {
        public MainUserControlSamples()
        {
            InitializeComponent();
        }

        IXvueSequencerUser sequencer;   
        //this is used to passed IXvueSequencerUser interface
        //to all the samples
        public void SetSequencer(IXvueSequencerUser seq)
        {
            if (seq != null)
            {
                sequencer = seq;
                radioButtonCSharp.Checked = true;
            }
            else //should close gracefully any plugins doing realtime jobs
            {
                ClearUI();
            }
        }

        //add a common user interface for the simple samples
        //using a combobox for selection and some buttons to enable actions
        private void AddSimpleSampleCSharp(SamplesCSharp.Simple.ISampleBase type)
        {
            type.SetSequencer(sequencer);
            comboBoxSimpleSamples.Items.Add(type);
        }

        private void AddSimpleSampleCPlusPlus(SamplesCPlusPlusManaged.ISampleBase type)
        {
            type.SetSequencer(sequencer);
            comboBoxSimpleSamples.Items.Add(type);
        }

        class helpercomboboxitem
        {
            public override string ToString()
            {
                return control.ToString();
            }
            public xvuePluginUI control;
        }

        //this simply adds tabs to the proper tabcontrol
        //we could do the same function more dynamic with a plugin style auto creation/load
        //but it would render this sample too complicated for its purpose
        private void AddOwnUISampleCSharp(xvuePluginUI control)
        {
            helpercomboboxitem item = new helpercomboboxitem();
            item.control = control;
            control.xSequencer = sequencer;
            comboBoxAdvancedSamples.Items.Add(item);
        }


        void ClearUI()
        {
            comboBoxSimpleSamples.Items.Clear();
            comboBoxAdvancedSamples.Items.Clear();
            panelDockPlugin.Controls.Clear();
            comboBoxSimpleSamples.SelectedIndex = -1;
            comboBoxAdvancedSamples.SelectedIndex = -1;
        
        }

        private void radioButtonCSharp_CheckedChanged(object sender, EventArgs e)
        {
            ClearUI();
            //add simple user interfaces with common UI
            AddSimpleSampleCSharp(new SamplesCSharp.Simple.HelloWorldText());

            //add samples with own UI
            AddOwnUISampleCSharp(new SamplesCSharp.AdvancedPlugins.HelloWorldAdvanced());
            AddOwnUISampleCSharp(new SamplesCSharp.AdvancedPlugins.LiveAndDVEAdvanced());
            AddOwnUISampleCSharp(new SamplesCSharp.AdvancedPlugins.LoadImageAnimationAdvanced());
        }

        private void radioButtonVBNET_CheckedChanged(object sender, EventArgs e)
        {
            ClearUI();
        }

        private void radioButtonCPlusPlusManaged_CheckedChanged(object sender, EventArgs e)
        {
            ClearUI();
            AddSimpleSampleCPlusPlus(new SamplesCPlusPlusManaged.SimpleHelloWorld());
        }

        private void buttonAction1_Click(object sender, EventArgs e)
        {
            if (comboBoxSimpleSamples.SelectedItem != null)
            {
                if (comboBoxSimpleSamples.SelectedItem is SamplesCPlusPlusManaged.ISampleBase)
                {
                    (comboBoxSimpleSamples.SelectedItem as SamplesCPlusPlusManaged.ISampleBase).HelperDoAction(0);
                }
                else if (comboBoxSimpleSamples.SelectedItem is SamplesCSharp.Simple.ISampleBase)
                {
                    (comboBoxSimpleSamples.SelectedItem as SamplesCSharp.Simple.ISampleBase).HelperDoAction(0);
                }
            }
        }

        private void comboBoxAdvancedSamples_SelectedIndexChanged(object sender, EventArgs e)
        {
            //I should do it more gracefull
            panelDockPlugin.Controls.Clear();
            if (comboBoxAdvancedSamples.SelectedItem != null)
            {
                xvuePluginUI plugin = (comboBoxAdvancedSamples.SelectedItem as helpercomboboxitem).control;
                panelDockPlugin.Controls.Add(plugin);
                plugin.Dock = DockStyle.Fill;
            }
        }

    }
}
